<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:57
 */

namespace app\admin\controller;


use app\admin\model\Order;
use app\api\service\Pay;

class OrderController extends BaseController
{
    /**
     * 订单详情
     * param order_id 订单id
     * returned object
     */

    public function OrderDetail($order_id)
    {
        // 关联order_detail发送数据到页面
        Order::autoConfirmOrder();
        $common_time = config('setting.auto_abate_time') * 60 * 60;
        $collage_time = config('setting.auto_collage_abate_time') * 60 * 60;
        Order::abateOrder(['status'=>1,'collage'=>1],$common_time);
        Order::abateOrder(['status'=>1,'collage'=>0],$collage_time);
        $result = Order::where('id', $order_id)->with('user,detail,coupon,address')->find();
        $this->assign('data',$result);
        return $this->fetch();
    }

    /**
     * 订单列表及按时间和手机号码查询
     * returned page
     */
    public function orderList()
    {
        if (request()->isPost()) {
            $post = request()->post();
            $data['time'] = [
                'start_time' => $post['start_time'],
                'end_time' => $post['end_time'],
            ];
            $data['value'] = [
                'type' => $post['order_type'],
                'status' => $post['order_status'],
                'is_comment' => $post['is_comment'],
            ];
            $data['like'] = [
                'phone' => $post['phone'],
            ];
            $where = Order::getSearch($data);
        }
        if(empty($where)) $where = null;
        $result = Order::where($where)->with('user,detail,coupon')->order('create_time','desc')->paginate(12, false, [
            'type' => 'page\Page',
        ]);
        $this->assign('data',$result);
        return $this->fetch();
    }

    public function checkPendingOrder()
    {
        $status = $this->request->param('status');
        $result = Order::where(['status'=>$status])->with('user,detail,coupon')->order('create_time','desc')->paginate(8,false,['type'=>'page\Page']);
        $this->assign('data',$result);
        return $this->fetch('order/order_list');
    }

    /**
     * type 23确认外卖订单并使订单进入配送中状态
     *      34 确认自取订单已领取
     *      24 确认外卖订单配送成功
     *      40 确认退款
     *      41 取消退款
     * 十位是type 个位是要修改的状态
     */
    public function changeStatus($order_id, $type)
    {
        if($type == 12){
            $re = Order::where(['id'=>$order_id])->update(['status'=>3]);
        }elseif($type == 15){
            //type=15商家审核微信支付的退款
            $pay = new Pay();
            $r = $pay->changeStatus($order_id);
            if($r){
                //请求退款成功,修改为退款中
                $re = Order::where('id', $order_id)->update(['status' => 6]);
            }else{
                return ajaxMsg('2', '退款接口异常，请联系技术支持');
            }
        }else{
            $re = false;
        }
        if ($re) {
            return ajaxMsg('1', '订单状态修改成功');
        } else {
            return ajaxMsg('2', '订单状态修改失败');
        }
//        if ($type == 23) {
//            $re = Order::where('id', $order_id)->update(['status' => 3]);
//        } elseif($type == 34) {
//            $re = Order::where('id', $order_id)->update(['status' => 4]);
//        }elseif ($type == 40){
//            //退款接口
//            $pay = new Pay();
//            $r = $pay->changeStatus($order_id);
//            if($r){
//                //请求退款成功,修改为退款中
//                $re = Order::where('id', $order_id)->update(['status' => 6]);
//            }else{
//                return ajaxMsg('2', '退款接口异常，请联系技术支持');
//            }
//        }elseif ($type == 41){
//            $re = Order::where('id', $order_id)->update(['status' => 3]);
//        }
//        else{
//            // 24
//            $re = Order::where('id', $order_id)->update(['status' => 4]);
//        }
//        if ($re) {
//            return ajaxMsg('1', '状态修改成功');
//        } else {
//            return ajaxMsg('2', '状态修改失败');
//        }
    }

    /**
     * 同意外卖订单的退款
     */
//    public function acceptRefund()
//    {
//
//    }

    /**
     * 拒绝外卖订单的退款并调整状态为配送中
     */
//    public function refuseRefund()
//    {
//
//    }

    /**
     * 订单批量加入历史订单
     */
//    public function orderAddHistory()
//    {
//
//    }
    /**
     * 查询某个用户的所有订单
     */
    public function getUserOrder($user_id)
    {
        $result =  Order::where('user_id',$user_id)->order('create_time','Asc')->paginate(25, false, ['type' => 'page\Page',]);
        return view('user_order', ['data' => $result]);
    }
}